<template>
  <el-dialog
    :title="!dataForm.patientId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="钱包" prop="patientMoney">
      <el-input v-model="dataForm.patientMoney" placeholder="钱包"></el-input>
    </el-form-item>
    <el-form-item label="患者姓名" prop="patientName">
      <el-input v-model="dataForm.patientName" placeholder="患者姓名"></el-input>
    </el-form-item>
    <el-form-item label="手机号" prop="cellPhone">
      <el-input v-model="dataForm.cellPhone" placeholder="手机号"></el-input>
    </el-form-item>
    <el-form-item label="密码" prop="password">
      <el-input v-model="dataForm.password" placeholder="密码"></el-input>
    </el-form-item>
    <el-form-item label="用户头像" prop="userAvatar">
      <el-input v-model="dataForm.userAvatar" placeholder="用户头像"></el-input>
    </el-form-item>
    <el-form-item label="收货地址" prop="shippingAddress">
      <el-input v-model="dataForm.shippingAddress" placeholder="收货地址"></el-input>
    </el-form-item>
    <el-form-item label="注册时间" prop="startTime">
      <el-input v-model="dataForm.startTime" placeholder="注册时间"></el-input>
    </el-form-item>
    <el-form-item label="逻辑删除 0：删除  1：不删除" prop="del">
      <el-input v-model="dataForm.del" placeholder="逻辑删除 0：删除  1：不删除"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          patientId: 0,
          patientMoney: '',
          patientName: '',
          cellPhone: '',
          password: '',
          userAvatar: '',
          shippingAddress: '',
          startTime: '',
          del: ''
        },
        dataRule: {
          patientMoney: [
            { required: true, message: '钱包不能为空', trigger: 'blur' }
          ],
          patientName: [
            { required: true, message: '患者姓名不能为空', trigger: 'blur' }
          ],
          cellPhone: [
            { required: true, message: '手机号不能为空', trigger: 'blur' }
          ],
          password: [
            { required: true, message: '密码不能为空', trigger: 'blur' }
          ],
          userAvatar: [
            { required: true, message: '用户头像不能为空', trigger: 'blur' }
          ],
          shippingAddress: [
            { required: true, message: '收货地址不能为空', trigger: 'blur' }
          ],
          startTime: [
            { required: true, message: '注册时间不能为空', trigger: 'blur' }
          ],
          del: [
            { required: true, message: '逻辑删除 0：删除  1：不删除不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.patientId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.patientId) {
            this.$http({
              url: this.$http.adornUrl(`/patient/patienttable/info/${this.dataForm.patientId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.patientMoney = data.patientTable.patientMoney
                this.dataForm.patientName = data.patientTable.patientName
                this.dataForm.cellPhone = data.patientTable.cellPhone
                this.dataForm.password = data.patientTable.password
                this.dataForm.userAvatar = data.patientTable.userAvatar
                this.dataForm.shippingAddress = data.patientTable.shippingAddress
                this.dataForm.startTime = data.patientTable.startTime
                this.dataForm.del = data.patientTable.del
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/patient/patienttable/${!this.dataForm.patientId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'patientId': this.dataForm.patientId || undefined,
                'patientMoney': this.dataForm.patientMoney,
                'patientName': this.dataForm.patientName,
                'cellPhone': this.dataForm.cellPhone,
                'password': this.dataForm.password,
                'userAvatar': this.dataForm.userAvatar,
                'shippingAddress': this.dataForm.shippingAddress,
                'startTime': this.dataForm.startTime,
                'del': this.dataForm.del
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
